package code.classic150on2025;

public class TheBit {
    // 137. 只出现一次的数字 II
    public int singleNumber(int[] nums) {
        int ans = 0;
        for (int i = 0; i < 32; i++) {
            int sum = 0;
            for (int num : nums) {
                sum += (num >> i) & 1;
            }
            if (sum % 3 != 0)
                ans |= (1 << i);
        }
        return ans;
    }

    // 201. 数字范围按位与
    public int rangeBitwiseAnd(int left, int right) {
        int shift = 0;
        // 0 <= left <= right <= 2^31 - 1
        // 不断右移left和right，直到让他们相等
        while (left != right) {
            left >>= 1;
            right >>= 1;
            shift++;
        }
        return left << shift;
    }
}
